package com.pgytesting.secondhandcar.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pgytesting.secondhandcar.model.McpCarDealerInformation;
import com.pgytesting.secondhandcar.model.McpUserInfo;
import com.pgytesting.secondhandcar.model.Power;
import com.pgytesting.secondhandcar.model.Role;
import com.pgytesting.secondhandcar.service.McpCarDealerInformationService;
import com.pgytesting.secondhandcar.service.McpUserInfoService;
import com.pgytesting.secondhandcar.service.RolePowerRelationService;
import com.pgytesting.secondhandcar.service.RoleService;
import com.pgytesting.secondhandcar.vo.ResponseMessage;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/admin/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private RolePowerRelationService rolePowerRelationService;
	@Autowired
	private McpCarDealerInformationService mcpCarDealerInformationService;
	@Autowired
	private McpUserInfoService mcpUserInfoService;
	
	/**
	 * 获取角色列表
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/getListObj",method = RequestMethod.POST)
	public ResponseMessage getListObj() {
		try {
			Role role = new Role();
			role.setStatus(0);
			return ResponseMessage.successResponse(roleService.queryAll(role));			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("获取角色列表出现异常");
		}
	}
	
	/**
	 * 获取角色详情
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/getObj",method = RequestMethod.POST)
	public ResponseMessage getObj(@RequestBody Role role) {
		try {
			return ResponseMessage.successResponse(roleService.queryObj(role));			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("获取角色详情出现异常");
		}
	}
	
	/**
	 * 添加角色信息
	 * @param role
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/insertObj",method = RequestMethod.POST)
	public ResponseMessage insertObj(@RequestBody @Valid Role role, BindingResult bindingResult) {
		try {
			if (bindingResult.hasErrors()) {
				List<FieldError> ls = bindingResult.getFieldErrors();
				Map<String,String> map = new HashMap<String,String>();
	            for (int i = 0; i < ls.size(); i++) {
	            	map.put(ls.get(i).getField(), ls.get(i).getDefaultMessage());
	            }
	            return ResponseMessage.errorResponse(map);
	        }
			roleService.insert(role);
			return ResponseMessage.successResponse("添加角色数据成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("添加角色信息出现异常");
		}
	}
	
	/**
	 * 修改角色信息
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/updateObj",method = RequestMethod.POST)
	public ResponseMessage updateObj(@RequestBody Role role) {
		try {
			if(role.getId() == null) {
				return ResponseMessage.errorResponse("权限id不能为空");
			}
			roleService.update(role);
			return ResponseMessage.successResponse("修改角色数据成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("修改角色信息出现异常");
		}
	}
	
	/**
	 * 删除角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/deleteObj",method = RequestMethod.POST)
	public ResponseMessage deleteObj(@RequestBody Role role) {
		try {
			if(role.getId() == null) {
				return ResponseMessage.errorResponse("权限id不能为空");
			}
			roleService.deleteObj(role.getId());
			return ResponseMessage.successResponse("删除角色数据成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("删除角色出现异常");
		}
	}
	
	/**
	 * 角色关联权限
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/roleRelationPower",method = RequestMethod.POST)
	public ResponseMessage roleRelationPower(@RequestBody Role role) {
		try {
			if(role.getId() == null) {
				return ResponseMessage.errorResponse("权限id不能为空");
			}
			rolePowerRelationService.roleRelationPower(role);
			return ResponseMessage.successResponse("角色关联权限成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("角色关联权限出现异常");
		}
	}
	
	/**
	 * 角色关联用户
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/roleRelationUser",method = RequestMethod.POST)
	public ResponseMessage roleRelationUser(@RequestBody McpUserInfo mcpUserInfo) {
		try {
			if(mcpUserInfo.getRoleId() == null) {
				return ResponseMessage.errorResponse("角色id不能为空");
			}
			if(mcpUserInfo.getUserNumbers() == null) {
				return ResponseMessage.errorResponse("关联账号不能为空");
			}
			roleService.updateUserRole(mcpUserInfo);
			return ResponseMessage.successResponse("角色关联用户成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("角色关联用户出现异常");
		}
	}
	
	/**
	 * 角色关联销售
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/roleRelationSale",method = RequestMethod.POST)
	public ResponseMessage roleRelationSale(@RequestBody McpCarDealerInformation mcpCarDealerInformation) {
		try {
			if(mcpCarDealerInformation.getRoleId() == null) {
				return ResponseMessage.errorResponse("角色id不能为空");
			}
			if(mcpCarDealerInformation.getUserNumbers() == null) {
				return ResponseMessage.errorResponse("关联账号不能为空");
			}
			roleService.updateSaleRole(mcpCarDealerInformation);
			return ResponseMessage.successResponse("角色关联销售成功");			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("角色关联销售出现异常");
		}
	}
	
	/**
	 * 获取销售列表
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/getSaleList",method = RequestMethod.POST)
	public ResponseMessage getSaleList() {
		try {
			McpCarDealerInformation mcpCarDealerInformation = new McpCarDealerInformation();
			mcpCarDealerInformation.setStatus(0);
			return ResponseMessage.successResponse(mcpCarDealerInformationService.queryAll(mcpCarDealerInformation));			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("获取销售列表出现异常");
		}
	}
	
	/**
	 * 获取用户列表
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/getUserList",method = RequestMethod.POST)
	public ResponseMessage getUserList() {
		try {
			McpUserInfo mcpUserInfo = new McpUserInfo();
			mcpUserInfo.setStatus(0);
			return ResponseMessage.successResponse(mcpUserInfoService.queryAll(mcpUserInfo));			
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("获取用户列表出现异常");
		}
	}
}
